package com.suning.sawp.service.impl.taskCard;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.intf.mainpushnew.MainPushClientService;
import com.suning.sawp.intf.taskCard.IndexTaskCardService;
import com.suning.sawp.intf.taskCard.dto.TaskCardDto;
import com.suning.sawp.intf.taskCard.dto.TaskCardDto.TaskStatus;
import com.suning.sawp.intf.taskCard.vo.TaskCardResponse;
import com.suning.sawp.intf.taskCard.vo.TaskCardVo;

/**
 * 首页任务卡服务实现 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class IndexTaskCardServiceImpl implements IndexTaskCardService {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexTaskCardServiceImpl.class);

    /**
     * 数据库Dal对象
     */
    @Autowired
    DalClient dalClient;
    
    /**
     * 主推服务
     */
    @Autowired
    MainPushClientService mainPushClientService;

    /**
     * 保存任务卡信息
     */
    @Override
    public void saveTaskCard(TaskCardDto dto) {
        LOGGER.info("saveTaskCard enter, TaskCardDto={}", dto);
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("staffId", dto.getStaffId());
        paramMap.put("bizKey", dto.getBizKey());
        paramMap.put("cardType", dto.getCardType());
        paramMap.put("title", dto.getTitle());
        paramMap.put("startTime", dto.getStartTime());
        paramMap.put("endTime", dto.getEndTime());

        /**
         * 第一个数值
         */
        if (StringUtils.isNotBlank(dto.getFirstNum())) {
            paramMap.put("firstNum", dto.getFirstNum());
        }

        /**
         * 第二个数值
         */
        if (StringUtils.isNotBlank(dto.getSecondNum())) {
            paramMap.put("secondNum", dto.getSecondNum());
        }

        /**
         * 第三个数值
         */
        if (StringUtils.isNotBlank(dto.getThirdNum())) {
            paramMap.put("thirdNum", dto.getThirdNum());
        }

        /**
         * 任务状态不为空
         */
        if (StringUtils.isNotBlank(dto.getTaskStatus())) {
            paramMap.put("taskStatus", dto.getTaskStatus());
        }
        /**
         * wap地址不为空
         */
        if (StringUtils.isNotBlank(dto.getWapLink())) {
            paramMap.put("wapLink", dto.getWapLink());
        }
        /**
         * 提示信息不为空
         */
        if (StringUtils.isNotBlank(dto.getTaskTips())) {
            paramMap.put("taskTips", dto.getTaskTips());
        }
        /**
         * 提成不为空
         */
        if (StringUtils.isNotBlank(dto.getPartnumberAmt())) {
            paramMap.put("partnumberAmt", dto.getPartnumberAmt());
        }
        /**
         * 品类编码不为空
         */
        if (StringUtils.isNotBlank(dto.getCateCode())) {
            paramMap.put("cateCode", dto.getCateCode());
        }

        /**
         * 扩展1
         */
        if (StringUtils.isNotBlank(dto.getExt1())) {
            paramMap.put("ext1", dto.getExt1());
        }

        /**
         * 扩展2
         */
        if (StringUtils.isNotBlank(dto.getExt2())) {
            paramMap.put("ext2", dto.getExt2());
        }

        /**
         * 扩展3
         */
        if (StringUtils.isNotBlank(dto.getExt3())) {
            paramMap.put("ext3", dto.getExt3());
        }

        /**
         * 扩展4
         */
        if (StringUtils.isNotBlank(dto.getExt4())) {
            paramMap.put("ext4", dto.getExt4());
        }

        /**
         * 扩展5
         */
        if (StringUtils.isNotBlank(dto.getExt5())) {
            paramMap.put("ext5", dto.getExt5());
        }
        dalClient.execute("TASK_CARD.INSERT", paramMap);
        LOGGER.info("saveTaskCard exit, TaskCardDto={}", dto);
    }

    /**
     * 删除任务卡信息
     */
    @Override
    public void delTaskCard(String staffId, String bizKey) {
        LOGGER.info("delTaskCard enter, staffId={},bizKey={}", staffId, bizKey);
        Map<String, Object> paramMap = Maps.newHashMap();
        // 业务类型
        paramMap.put("bizKey", bizKey);
        /**
         * 店员ID不为空
         */
        if (StringUtils.isNotBlank(staffId)) {
            paramMap.put("staffId", staffId);
        }
        try {
            dalClient.execute("TASK_CARD.DELETE", paramMap);
        } catch (Exception e) {
            LOGGER.error("delTaskCard SQL error, staffId=" + staffId + ",bizKey=" + bizKey, e);
        }
        LOGGER.info("delTaskCard exit, staffId={},bizKey={}", staffId, bizKey);
    }

    /**
     * 查询店员任务卡列表
     */
    @Override
    public TaskCardResponse queryTaskCard(String staffId) {
        LOGGER.info("queryTaskCard enter, staffId={}", staffId);
        TaskCardResponse response = new TaskCardResponse();
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("staffId", staffId);
        // 取有效的任务卡
        paramMap.put("currTime", new Date());
        // 查询店员任务卡列表
        List<TaskCardDto> list = dalClient.queryForList("TASK_CARD.QUERY_TASK_CARD", paramMap, TaskCardDto.class);
        LOGGER.debug("queryTaskCard response list={}", list);
        // 查询主推任务列表
        List<TaskCardVo> mainList = mainPushClientService.queryMainPushTaskList(staffId);
        List<TaskCardVo> cards = new ArrayList<TaskCardVo>();
        // 添加任务
        cards.addAll(mainList);
        // 转换卡片数据
        cards.addAll(convertTaskCard(list));
        response.setList(cards);
        LOGGER.info("queryTaskCard exit, staffId={},response={}", staffId, response);
        return response;
    }

    /**
     * 转换卡片熟悉DTO -> VO 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param list
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private List<TaskCardVo> convertTaskCard(List<TaskCardDto> list) {
        // 判断卡片集合是否为空
        if (CollectionUtils.isNotEmpty(list)) {
            TaskCardVo vo = null;
            List<TaskCardVo> resp = new ArrayList<TaskCardVo>();
            for (TaskCardDto dto : list) {
                vo = new TaskCardVo();
                vo.setBizKey(dto.getBizKey());
                vo.setCardType(dto.getCardType());
                vo.setTitle(dto.getTitle());
                vo.setFirstNum(dto.getFirstNum());
                vo.setSecondNum(dto.getSecondNum());
                vo.setThirdNum(dto.getThirdNum());
                // 状态描述转换
                vo.setTaskStatus(getTaskStatusName(dto.getTaskStatus()));
                vo.setWapLink(dto.getWapLink());
                vo.setTaskTips(dto.getTaskTips());
                vo.setPartnumberAmt(dto.getPartnumberAmt());
                vo.setCateCode(dto.getCateCode());
                // wap页头信息标识,是否展示导航1是显示客户端头
                vo.setExt1(dto.getExt1());
                vo.setExt2(dto.getExt2());
                vo.setExt3(dto.getExt3());
                vo.setExt4(dto.getExt4());
                vo.setExt5(dto.getExt5());
                resp.add(vo);
            }
            return resp;
        }
        // 默认返回空集合
        return Collections.emptyList();
    }

    /**
     * 转换状态描述
     *
     * @param status
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String getTaskStatusName(String status) {
        LOGGER.info("getTaskStatusName enter,status={}", status);
        if (StringUtils.isNotBlank(status)) {
            for (TaskStatus type : TaskStatus.values()) {
                if (type.getId().equals(status)) {
                    LOGGER.debug("getTaskStatusName,status={},statusName={}", status, type.getName());
                    return type.getName();
                }
            }
        }
        return null;
    }
}
